package fabrik;

import java.util.logging.Logger;

/**
 * This method contains the relations and implements Comparable<Relations>.
 * 
 * @author Janet
 * @version 1.0
 */
public class Relations implements Comparable<Relations> {
	/**
	 * These are the words which we are looking for to search for the relations.
	 */
	private static Logger log = Logger.getLogger(Relations.class.getName());

	/** the relation EA. */
	private static final String EA = new String("EA");
	/** the relation AA. */
	private static final String AA = new String("AA");
	/** the relation AE. */
	private static final String AE = new String("AE");
	/** the relation EE. */
	private static final String EE = new String("EE");

	/**
	 * An enum for the relation types.
	 * @author Janet
	 * 
	 */
	public enum RelationType {
		/**type EA*/
		EA,
		/**type AA*/
		AA,
		/**type AE*/
		AE,
		/**type EE*/
		EE
	}

	/** the pre process. */
	private Process pre;
	/** the succ process. */
	private Process successor;
	/** the string for the relation. */
	private RelationType type;

	/**
	 * The constructor of this class. It contains predecessor,successor and a
	 * relation type.
	 * 
	 * @param pre
	 *            is the predecessor of the process
	 * @param successor
	 *            is the successor of the process
	 * @param type
	 *            shows the relation between the two processes
	 */
	public Relations(Process pre, Process successor, String type) {
		this.pre = pre;
		this.successor = successor;
		// checks the relation
		if (type.equalsIgnoreCase(EA))
			this.type = RelationType.EA;
		else if (type.equalsIgnoreCase(AA))
			this.type = RelationType.AA;
		else if (type.equalsIgnoreCase(AE))
			this.type = RelationType.AE;
		else if (type.equalsIgnoreCase(EE))
			this.type = RelationType.EE;
		// if the type is not one of the four known
		else
			log.warning("Relation type cannot be recognized.");

	}

	/**
	 * get the predecessor of the process.
	 * 
	 * @return pre, predecessor
	 */
	public Process getPre() {
		return pre;
	}

	/**
	 * sets the predecessor of the process.
	 * 
	 * @param pre
	 *            ,the predecessor
	 */
	public void setPre(Process pre) {
		this.pre = pre;
	}

	/**
	 * gets the successor of the process.
	 * 
	 * @return successor of processor
	 */
	public Process getSuccessor() {
		return successor;
	}

	/**
	 * sets the successor of the process.
	 * 
	 * @param successor
	 *            of the process
	 */
	public void setSucessor(Process successor) {
		this.successor = successor;
	}

	/**
	 * gets the relation of the process.
	 * 
	 * @return relation of the process
	 */
	public RelationType getType() {
		return type;
	}

	/**
	 * sets the relation of the process.
	 * 
	 * @param type
	 *            ,relation
	 */
	public void setType(RelationType type) {
		this.type = type;
	}

	/**
	 * A String to String method, prints out the predecessors and successors.
	 * @return the pre,succ and type of each element of relation
	 */
	@Override
	public String toString() {
		return "Relations [pre=" + pre.getID() + ", successor="
				+ successor.getID() + ", type=" + type + "]";

	}

	/**
	 * Comparison.
	 * 
	 * @param o
	 *            the relation
	 * @return the difference of the ids.
	 */
	@Override
	public int compareTo(Relations o) {
		return pre.getID() - o.pre.getID();
	}

}
